﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;
using System.Data.SqlClient;
using System.Globalization;
using System.Data;

namespace University.Workshop.DataAccess
{
    public static class GiftCardDataAccess
    {
        private const string ConnectionString = "Data Source=.\\sqlexpress; Initial Catalog=LQSdataBase; Integrated Security=true";

        public static GiftCard NewGiftCard(GiftCard giftCard)
        {
            if (giftCard != null)
            {
                SqlConnection connection = null;
                try
                {
                    connection = new SqlConnection(ConnectionString);
                    SqlCommand command = connection.CreateCommand();
                    command.CommandText = "INSERT INTO GIFTCARD (UserIdSender,UserIdRecipient,Quantity,Currency,Notificated,Available) VALUES(@UserIdSender,@UserIdRecipient,@Quantity,@Currency,@Notificated,@Available)";
                    command.Parameters.AddWithValue("UserIdSender", giftCard.UserIdSender);
                    command.Parameters.AddWithValue("UserIdRecipient",giftCard.UserIdRecipient);
                    command.Parameters.AddWithValue("Quantity",giftCard.Quantity);
                    command.Parameters.AddWithValue("Currency",giftCard.Currency);
                    command.Parameters.AddWithValue("Notificated",giftCard.Notification);
                    command.Parameters.AddWithValue("Available",giftCard.Available);
                    SqlCommand getID = connection.CreateCommand();
                    getID.CommandText = "SELECT @@Identity";

                    connection.Open();
                    int result = command.ExecuteNonQuery();
                    if (result <= 0)
                    {
                        throw new DataAccessException("Error inserting a gift card");
                    }
                    giftCard.GiftCardId = Convert.ToInt64(getID.ExecuteScalar(), CultureInfo.CurrentCulture);
                }
                catch (SqlException ex)
                {
                    throw new DataAccessException("Error inserting a gift card", ex);
                }
                finally
                {
                    if (connection != null)
                    {
                        connection.Close();
                    }
                    SqlConnection.ClearAllPools();
                }
            }

            return giftCard;
        }
        public static void  ModifyQuantity(long giftCardId,float quantity)
        {
            SqlConnection connection = null;
            connection = new SqlConnection(ConnectionString);
            try
            {
                SqlCommand command = connection.CreateCommand();
                command.CommandText = "UPDATE GIFTCARD SET Quantity=@quantity WHERE GiftCardId=@giftCardId";
                command.Parameters.AddWithValue("Quantity", quantity);
                command.Parameters.AddWithValue("GiftCardId", giftCardId);
                connection.Open();
                int result = command.ExecuteNonQuery();
                if (result <= 0)
                {
                    throw new DataAccessException("Error updating quantity of giftcard");
                }
            }
            catch (SqlException ex)
            {
                throw new DataAccessException("Error updating quantity of giftcard", ex);
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
                SqlConnection.ClearAllPools();
            }
        }
        
        public static GiftCard ModifyGiftCard(GiftCard giftCard)
        {
            if (giftCard != null)
            {
                SqlConnection connection = null;
                try
                {
                    connection = new SqlConnection(ConnectionString);
                    SqlCommand command = connection.CreateCommand();
                    command.CommandText = "UPDATE GIFTCARD SET Quantity=@quantity WHERE GiftCardId=@giftCardId";
                    command.Parameters.AddWithValue("Quantity", giftCard.Quantity);
                    command.Parameters.AddWithValue("Available", giftCard.Available);
                    command.Parameters.AddWithValue("GiftCardId",giftCard.GiftCardId);
                  
                    connection.Open();
                    int result = command.ExecuteNonQuery();
                    if (result <= 0)
                    {
                        throw new DataAccessException("Error modifying a gift card");
                    }
                }
                catch (SqlException ex)
                {
                    throw new DataAccessException("Error modifying a gift card", ex);
                }
                finally
                {
                    if (connection != null)
                    {
                        connection.Close();
                    }
                    SqlConnection.ClearAllPools();
                }
            }

            return giftCard;
        }

        public static bool UserHasNewCredit(User recipient)
        {
            if (recipient != null)
            {
                SqlConnection co = null;
                try
                {
                    co = new SqlConnection(ConnectionString);
                    SqlCommand cmd = co.CreateCommand();
                    cmd.CommandText = "SELECT * FROM GIFTCARD Where UserIdRecipient=@userIdRecipient and Notificated='no'";
                    cmd.Parameters.AddWithValue("UserIdRecipient", recipient.Id);
                    co.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (!reader.HasRows)
                    {
                        return false;
                    }
                    return true;
                }
                catch (SqlException ex)
                {
                    throw new DataAccessException("Error getting recipient user from giftcard", ex);
                }
                finally
                {
                    if (co != null)
                    {
                        co.Close();
                    }
                    SqlConnection.ClearAllPools();
                }
            }
            else
                return false;
        }
        
        public static GiftCard GetGiftCardByRecipientId(long userIdRecipient)
        {
            GiftCard giftcard = new GiftCard();
            SqlConnection connection = null;
            try
            {
                connection = new SqlConnection(ConnectionString);
                SqlCommand command = connection.CreateCommand();
                command.CommandText = "SELECT * FROM GIFTCARD WHERE UserIdRecipient=@userIdRecipient";
                command.Parameters.AddWithValue("UserIdRecipient", userIdRecipient);
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                if (!reader.HasRows)
                {
                    return null;
                }
                while (reader.Read())
                {
                    giftcard.GiftCardId = Int64.Parse(reader["GiftCardId"].ToString(),CultureInfo.CurrentCulture);
                    giftcard.UserIdSender = Int64.Parse(reader["UserIdSender"].ToString(), CultureInfo.CurrentCulture);
                    //giftcard.UserIdRecipient = Int64.Parse(reader["UserIdRecipient"].ToString(), CultureInfo.CurrentCulture);
                    giftcard.UserIdRecipient = userIdRecipient;
                    giftcard.Quantity = float.Parse(reader["Quantity"].ToString(), CultureInfo.CurrentCulture);
                    giftcard.Currency = reader["Currency"].ToString();
                    giftcard.Notification = reader["Notificated"].ToString();
                    giftcard.Available = reader["Available"].ToString();
                }
            }
            catch (SqlException ex)
            {
                throw new DataAccessException("Error getting a giftcard", ex);
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
                SqlConnection.ClearAllPools();
            }
            return giftcard;
        }
        public static void NotificationGiftCardToYes(GiftCard giftCard)
        {
            if (giftCard != null)
            {
                SqlConnection connection = null;
                connection = new SqlConnection(ConnectionString);
                try
                {
                    SqlCommand command = connection.CreateCommand();
                    command.CommandText = "UPDATE GIFTCARD SET Notificated='Yes' WHERE GiftCardId=@giftCardId";
                    command.Parameters.AddWithValue("GiftCardId", giftCard.GiftCardId);
                    connection.Open();
                    int result = command.ExecuteNonQuery();
                    if (result <= 0)
                    {
                        throw new DataAccessException("Error updating giftcard");
                    }
                }
                catch (SqlException ex)
                {
                    throw new DataAccessException("Error updating giftcard", ex);
                }
                finally
                {
                    if (connection != null)
                    {
                        connection.Close();
                    }
                    SqlConnection.ClearAllPools();
                }
            }
        
        }

        public static object GetGiftCardById(long giftCardId)
        {
            SqlConnection connection = null;
            DataView dataView = null;
            DataSet ds = new DataSet();
            SqlDataAdapter adapter = null;
            try
            {

                ds.Locale = CultureInfo.CurrentCulture;
                connection = new SqlConnection(ConnectionString);
                SqlCommand command = connection.CreateCommand();
                command.CommandText = "SELECT Quantity,Currency FROM GIFTCARD WHERE GiftCardId=@giftCardId ";
                command.Parameters.AddWithValue("GiftCardId", giftCardId);
                connection.Open();
                adapter = new SqlDataAdapter(command);
                adapter.Fill(ds, "GIFTCARD");
                if (ds.HasErrors) ds.RejectChanges(); else ds.AcceptChanges();
                dataView = ds.Tables["GIFTCARD"].DefaultView;
                return dataView;

            }
            catch (SqlException ex)
            {
                throw new DataAccessException("Error getting giftcard quantity and currency", ex);
            }
            finally
            {
                if (connection != null)
                {
                    connection.Close();
                }
                if (ds != null)
                {
                    ds.Dispose();
                }
                if (adapter != null)
                {
                    adapter.Dispose();
                }
                SqlConnection.ClearAllPools();
            }
        }


    }
}
